package handler

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"lucidity.note/app/common/bean"
	"lucidity.note/app/service"
)

func RegisterUserSettingRouter(router *gin.Engine) {
	group := router.Group("/user/setting")
	{
		group.GET("/appearance", getAppearanceSettingHandler)
		group.PUT("/update/appearance", updateAppearanceSettingHandler)
		group.GET("/gitsync", getGitSyncSettingHandler)
		group.PUT("/update/gitsync", updateGitSyncSettingHandler)
		group.GET("/resources", getConfResourceFileInfoHandler)
		group.GET("/note/textinfo", getNoteTextConfHandler)
		group.PUT("/update/textinfo", updateNoteTextConfHandler)
	}
}

func getAppearanceSettingHandler(ctx *gin.Context) {
	service := &service.UserSettingService{}
	response := &bean.ResponseParam{}
	data := service.GetAppearanceConfigService()
	response.Data = data
	ctx.JSON(response.Status, response)
}

func updateAppearanceSettingHandler(ctx *gin.Context) {
	var appearance bean.AppearanceConfig
	response := &bean.ResponseParam{}
	if err := ctx.ShouldBind(&appearance); nil != err {
		response.Failure(fmt.Sprintf("更新外观设置失败，错误详情{%s}", err.Error()))
	} else {
		service := &service.UserSettingService{}
		if err := service.UpdateAppearanceSettingService(&appearance); nil != err {
			response.Failure(err.Error())
		} else {
			response.Succeed("更新外观设置成功")
		}
	}
	ctx.JSON(response.Status, response)
}

func getGitSyncSettingHandler(ctx *gin.Context) {
	service := &service.UserSettingService{}
	response := &bean.ResponseParam{}
	data := service.GetGitSyncSettingConfigService()
	response.Data = data
	ctx.JSON(response.Status, response)
}

func updateGitSyncSettingHandler(ctx *gin.Context) {
	var gitsync bean.GitSyncConfig
	response := &bean.ResponseParam{}
	if err := ctx.ShouldBind(&gitsync); nil != err {
		response.Failure(fmt.Sprintf("更新Git同步设置失败，错误详情{%s}", err.Error()))
	} else {
		service := &service.UserSettingService{}
		if err := service.UpdateGitSyncSettingService(&gitsync); nil != err {
			response.Failure(err.Error())
		} else {
			response.Succeed("更新Git同步设置成功")
		}
	}
	ctx.JSON(response.Status, response)
}

func getConfResourceFileInfoHandler(ctx *gin.Context) {
	service := &service.UserSettingService{}
	response := &bean.ResponseParam{}
	data := service.GetConfResourceFileInfoService()
	response.Data = data
	response.Succeed("更新Git同步设置成功")
	ctx.JSON(response.Status, response)
}

func getNoteTextConfHandler(ctx *gin.Context) {
	service := &service.UserSettingService{}
	response := &bean.ResponseParam{}
	data := service.GetNoteTextConfService()
	response.Data = data
	ctx.JSON(response.Status, response)
}

func updateNoteTextConfHandler(ctx *gin.Context) {
	var noteTextConf bean.NoteTextConf
	response := &bean.ResponseParam{}
	if err := ctx.ShouldBind(&noteTextConf); nil != err {
		response.Failure(fmt.Sprintf("更新文本设置失败，错误详情{%s}", err.Error()))
	} else {
		service := &service.UserSettingService{}
		if err := service.UpdateNoteTextConfService(&noteTextConf); nil != err {
			response.Failure(err.Error())
		} else {
			response.Succeed("更新文本信息设置成功")
		}
	}
	ctx.JSON(response.Status, response)
}
